package org.gl.dialog;

import org.gl.GlobalVariable;
import org.gl.android.base.R;
import org.gl.utils.AndroidUtil;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;


public class ButtonsPopuWindow {
	PopupWindow popup;
	Context context;
	int heigh = AndroidUtil.dipToPx(250);
	int weith = AndroidUtil.dipToPx(128);
	public ButtonsPopuWindow(Context c) {
		this.context = c;
	};

	@SuppressWarnings("deprecation")
	public void showWindow(View parent) {
		if (popup == null) {
			LayoutInflater layoutInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View view = layoutInflater.inflate(R.layout.popuwindows_menu, null);
			if (mOnClickListener != null) {
				view.findViewById(R.id.button1).setOnClickListener(
						mOnClickListener);
				view.findViewById(R.id.button2).setOnClickListener(
						mOnClickListener);
				view.findViewById(R.id.button3).setOnClickListener(
						mOnClickListener);
				view.findViewById(R.id.button4).setOnClickListener(
						mOnClickListener);
				view.findViewById(R.id.button5).setOnClickListener(
						mOnClickListener);
			}
			// 创建一个PopuWidow对象
			popup = new PopupWindow(view,
					weith,
					heigh);
			// 设置焦点在弹窗上
			popup.setFocusable(true);
			// 设置允许在外点击消失
			popup.setOutsideTouchable(true);
			// 设置弹窗消失事件监听
			popup.setOnDismissListener(new OnDismissListener() {
				public void onDismiss() {
					// TODO Auto-generated method stub
					// image.setImageResource(R.drawable.toolbar_plus);
				}
			});
			// 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
//			new AndroidDrawableUtil().drawLayoutDropShadow(view,AndroidUtil.dipToPx(135),AndroidUtil.dipToPx(298));
			popup.setBackgroundDrawable(new ColorDrawable(0x00000000));
			popup.setTouchInterceptor(new OnTouchListener() {
				public boolean onTouch(View view, MotionEvent event) {
					if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
						popup.dismiss();
						// image.setImageResource(R.drawable.toolbar_plus);
						return true;
					}
					return false;
				}
			});
		}
		if (!popup.isShowing()) {
			popup.showAsDropDown(parent, GlobalVariable.disPlay_w-AndroidUtil.dipToPx(142), 0);
		}
	}

	View.OnClickListener mOnClickListener;

	public void setOnClick(View.OnClickListener mOnClickListener) {
		this.mOnClickListener = mOnClickListener;
	}
}
